package f.admin.service;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import f.admin.entity.DictionaryModule;
import f.admin.entity.DictionaryObject;
import f.admin.entity.QueryParam;
import f.orm.dao.BaseDao;
import f.tools.DepUtil;

public class DictionaryModuleService extends BaseDao implements IService<DictionaryModule> {
	
	public void add(DictionaryModule dictModule) throws Exception {
		String sql = "insert into " + DictionaryModule.TABLE_NAME + "(id,name,createTime)" +
				" values(" +
						"'" + DepUtil.createPrimaryId() + "'," +
						"'" + dictModule.getName() + "'," +
						"sysdate())";
		executeUpdate(sql, null);
	}
	
	public void update(DictionaryModule dictModule) throws Exception {
		String sql = "update " + DictionaryModule.TABLE_NAME + " set name='" + dictModule.getName() + "' where id='" + dictModule.getId() + "'";
		executeUpdate(sql, null);
	}
	
	@SuppressWarnings("finally")
	public DictionaryModule getById(String id) {
		DictionaryModule dictModule = new DictionaryModule();
		String sql = "select * from " + DictionaryModule.TABLE_NAME + " where id = '" + id + "'";
		try {
			getConnection();
			ResultSet rs = executeQuery(sql, null);
			while(rs.next()) {
				dictModule.setId(rs.getString("id"));
				dictModule.setName(rs.getString("name"));
				dictModule.setCreateTime(rs.getTimestamp("createTime"));
			}
		} catch(Exception e) {
			e.printStackTrace(); 
		} finally {
			close();
			return dictModule;
		}
	}
	
	public boolean delete(String id) {
		boolean isDelete = false;
		String sql = "delete from " + DictionaryModule.TABLE_NAME + " where id = '" + id + "'";
		try {
			executeUpdate(sql, null);
			isDelete = true;
		} catch(Exception e) {
			isDelete = false;
			e.printStackTrace();
		}
		return isDelete;
	}

	@SuppressWarnings("finally")
	public List<DictionaryModule> getByQueryParams(List<QueryParam> queryParams) {
		String sql = "select * from " + DictionaryObject.TABLE_NAME + " where ";
		for(int i = 0; i < queryParams.size(); i++) {
			QueryParam queryParam = queryParams.get(i);
			sql += queryParam.getQueryParamString();
			if(i < queryParams.size() - 1) {
				sql += " and ";
			}
		}
		sql += " order by createTime desc";
		List<DictionaryModule> dictModuleList = new ArrayList<DictionaryModule>();
		try {
			getConnection();
			ResultSet rs = executeQuery(sql, null);
			while(rs.next()) {
				DictionaryModule dictModule = new DictionaryModule();
				dictModule.setId(rs.getString("id"));
				dictModule.setName(rs.getString("name"));
				dictModule.setCreateTime(rs.getTimestamp("createTime"));
				dictModuleList.add(dictModule);
			}
		} catch(Exception e) {
			e.printStackTrace(); 
		} finally {
			close();
			return dictModuleList;
		}
	}
	
	@SuppressWarnings("finally")
	public List<DictionaryModule> getByQueryParam(QueryParam queryParam) {
		String sql = "select * from " + DictionaryObject.TABLE_NAME + " where ";
		sql += queryParam.getQueryParamString() + " order by createTime desc";
		List<DictionaryModule> dictModuleList = new ArrayList<DictionaryModule>();
		try {
			getConnection();
			ResultSet rs = executeQuery(sql, null);
			while(rs.next()) {
				DictionaryModule dictModule = new DictionaryModule();
				dictModule.setId(rs.getString("id"));
				dictModule.setName(rs.getString("name"));
				dictModule.setCreateTime(rs.getTimestamp("createTime"));
				dictModuleList.add(dictModule);
			}
		} catch(Exception e) {
			e.printStackTrace(); 
		} finally {
			close();
			return dictModuleList;
		}
	}
	
	@SuppressWarnings("finally")
	public List<DictionaryModule> listAll() {
		String sql = "select * from " + DictionaryModule.TABLE_NAME + " order by createTime desc";
		List<DictionaryModule> dictModuleList = new ArrayList<DictionaryModule>();
		try {
			getConnection();
			ResultSet rs = executeQuery(sql, null);
			while(rs.next()) {
				DictionaryModule dictModule = new DictionaryModule();
				dictModule.setId(rs.getString("id"));
				dictModule.setName(rs.getString("name"));
				dictModule.setCreateTime(rs.getTimestamp("createTime"));
				dictModuleList.add(dictModule);
			}
		} catch(Exception e) {
			e.printStackTrace(); 
		} finally {
			close();
			return dictModuleList;
		}
	}
	
	public JSONArray listToJsonArray(List<DictionaryModule> dictModuleList) {
		JSONArray jsonArr = new JSONArray();
		for(DictionaryModule dictModule : dictModuleList) {
			JSONObject jsonObj = JSONObject.fromObject(dictModule.getJsonString());
			jsonArr.add(jsonObj);
		}
		return jsonArr;
	}

	public List<DictionaryModule> listByPage(int page, int pageSize,
			QueryParam... queryParams) {
		return null;
	}

	public int getCount() {
		return 0;
	}
}
